<template>
    <a-card :bordered="false">
        <div class="table-operator">
            <a-button shape="round" size="small" type="primary" icon="plus" @click="handleAdd()">添加</a-button>
        </div>
        <a-spin :spinning="confirmLoading">
            <LTable
                ref="templateList"
                size="middle"
                rowKey="keyId"
                :formAttributes="formAttributes"
                :columns="columns"
                axiosUrl="/actReModel/list"
                :initQuery="[{field:'systemNo',val:defaultSettings.systemNo}]"
                :showPagination="true"
                :showQueryForm="true">
        
                <span slot="action" slot-scope="text, record">
                    <a @click="handleAdd(record)">编辑</a><a-divider type="vertical" />
                    <a @click="handleDeploy(record)">部署</a><a-divider type="vertical" />
                    <a @click="handleDelete(record)">删除</a><a-divider type="vertical" />
                    <a @click="handleClear(record)">清理历史</a>
                    <!--
                    <a-divider type="vertical" />
                    <a @click="handleStart(record)">发起</a><a-divider type="vertical" />
                    <a @click="handleCommit(record)">提交</a> -->
                </span>
            
            </LTable>
        </a-spin>
        <drawer-modeler ref="modeler" @handleCancel="refresh('templateList')"></drawer-modeler>
    </a-card>

    <!--
        
        truncate table ACT_HI_DETAIL;
        truncate table ACT_HI_ATTACHMENT;
        truncate table ACT_HI_COMMENT;
        truncate table ACT_HI_IDENTITYLINK;
        truncate table ACT_HI_PROCINST;
        truncate table ACT_HI_VARINST;
        truncate table ACT_HI_TASKINST ;
        truncate table ACT_HI_ACTINST;
        truncate table ACT_RU_IDENTITYLINK;
        truncate table ACT_RU_TASK;
        truncate table ACT_RU_VARIABLE;
        truncate table ACT_RU_EXECUTION;
        truncate table ACT_RE_PROCDEF;
        truncate table ACT_GE_BYTEARRAY;
        truncate table ACT_RE_DEPLOYMENT;
    -->
</template>

<script>
import LTable from '@/components/lt/LTable' 
import request from '@/utils/flowRequest'
import {SysMixin} from '@/mixins/SysMixin'
import drawerModeler from './drawerModeler';
import notification from 'ant-design-vue/es/notification'
import defaultSettings from '@/config/defaultSettings'

export default {
    name:'templateList',
    components:{
       LTable, drawerModeler
    },
    mixins:[SysMixin],
    data(){
        return{
            defaultSettings,
            request,
            confirmLoading: false,
            url: {
                deploy: '/actReModel/deployModel',
                delete: '/actReModel/deleteModel',
                clear: '/actReModel/clearModelHis'
            },
            formAttributes:[
                {field:'systemNo',label:'系统编号',component:'input',condition:'like'},
                {field:'templateName',label:'模板名称',component:'input',condition:'like'},
                {field:'keyId',label:'模板ID',component:'input',condition:'like'}
            ],
            columns:[
                {
                    title: '#',
                    dataIndex: '',
                    key:'rowIndex',
                    width:60,
                    align:"center",
                    customRender:function (t,r,index) {
                      return parseInt(index)+1;
                    }
                },
                {
                    title: '系统编号',
                    dataIndex: 'systemNo'
                },
                {
                    title: '模板ID',
                    dataIndex: 'keyId'
                    
                },
                {
                    title: '模板名称',
                    dataIndex: 'templateName'
                },
                {
                    title: '模板类型',
                    dataIndex: 'templateCategory'
                },
                {
                    title: '模板版本号',
                    dataIndex: 'templateVersion'
                },
                {
                    title: '创建时间',
                    dataIndex: 'createTime'
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    width: '220px',
                    scopedSlots: { customRender: 'action' }
                }
            ],
            
        }
    },
    created(){
       
    },
    methods:{
        handleAdd(record) {
            if (record && record.keyId) {
                this.$refs['modeler'].open(record.keyId)
            } else {
                this.$refs['modeler'].open()
            }
            
        },
        handleDeploy(record) {
            this.confirmLoading = true;
            request.get(this.url.deploy,{params:{keyId: record.keyId}}).then(res => {
                if(res.success){
                    this.$message.success(res.message);
                } else {
                    notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                }
            }).finally(() => {
                this.confirmLoading = false
            })
        },
        handleClear(record) {
            this.confirmLoading = true;
            request.get(this.url.clear,{params:{keyId: record.keyId}}).then(res => {
                if(res.success){
                    this.$message.success(res.message);
                } else {
                    notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                }
            }).finally(() => {
                this.confirmLoading = false
            })
        },
        handleDelete(record) {
            this.confirmLoading = true;
            request.get(this.url.delete,{params:{keyId: record.keyId}}).then(res => {
                if(res.success){
                    this.$message.success(res.message);
                } else {
                    notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                }
            }).finally(() => {
                this.confirmLoading = false
            })
        },
        handleStart(record) {
            let param = {}
            param.userId = '1'
            param.userName = '管理员'
            param.leaveNum = 5
            param.leaveReason = '医院看病'
            param.startTime = '2021-08-13 08:00:00'
            param.endTime = '2021-08-17 18:00:00'
            param.templateId = 'leave_test'
            param.taskId = '-1'
            this.confirmLoading = true;

            request({url: "/leaveInfo/commitForm", method: 'post', data:param}).then(res => {
                if(res.success){
                    this.$message.success(res.message);
                } else {
                    notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                }
            }).finally(() => {
                this.confirmLoading = false
            })
        },
        handleCommit(record) {
            let param = {}
            param.task = ""
            request({url: "/flowRuntime/commitTask", method: 'post', data:param}).then(res => {
                if(res.success){
                    this.$message.success(res.message);
                } else {
                    notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                }
            })
        }
    }
}
</script>